package com.ds.sign.controller;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/api/test")
public class TestController 
{
	@GetMapping("/getRemotIP")
	public @ResponseBody Object getRemortIP(HttpServletRequest request) 
	{
		String remoteIp = request.getRemoteAddr();
		return remoteIp;
	}
	
	@GetMapping("/getRemotIP2")
	public @ResponseBody Object getRemortIP2(HttpServletRequest request) 
	{
		try 
		{
			InetAddress ip = Inet4Address.getLocalHost();
			return ip;
		} 
		catch (UnknownHostException e) 
		{
			e.printStackTrace();
		}
		return "";
	}
	
	@GetMapping("/getRemotIP3")
	public @ResponseBody Object getRemortIP3(HttpServletRequest request) 
	{
		String remoteIp = "";
		if (request.getHeader("x-forwarded-for")  ==  null) 
		{
			remoteIp = request.getRemoteAddr();
		} 
		else 
		{
			remoteIp = request.getHeader("x-forwarded-for");
		}
		return remoteIp;
	}
}
